<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Project extends CI_Model
{
	function __construct()
	{
		parent::__construct();

	}

	// If $projectId is NULL, gets all posts, otherwise get single post from db 
	// returns $project[]
	public function get_project($projectId)
	{
		$project=array();
		if ($projectId !== null){
			$query = $this->db->get_where('project', array('id' => $projectId));
			if ($query->num_rows() == 1) {	
				foreach($query->result() as $row){
				$project['id'] = $row->id;
				$project['title'] = $row->title;
				$project['summary'] = $row->summary;
				$project['content'] = $row->content;
                $project['created'] = $row->created;
                $project['img_url'] = $row->img_url;
                $project['img_thumb_url'] = $row->img_thumb_url;
				}
				return $project;
			}	
		} else {
			$query = $this->db->get('project');
			if ($query->num_rows() !== 0 ){
				foreach($query->result() as $row){
				$project['id'][] = $row->id;
				$project['title'][] = $row->title;
				$project['summary'][] = $row->summary;
				$project['content'][] = $row->content;
                $project['created'][] = $row->created;
                $project['img_url'][] = $row->img_url;
                $project['img_thumb_url'][] = $row->img_thumb_url;
				}
				return $project;
			}
			
		}		
	}

	function insert_project($data){
        $data['updated'] = date('Y-m-d H:i:s');
		$this->db->insert('project', $data);
		return;
	}

	function update_project($projectId,$data){
		$id = $projectId;
        $data['updated'] = date('Y-m-d H:i:s');
       	$this->db->where('id',$id);
		$this->db->update('project', $data);
		return;
	}

	function delete_project($projectId){
		$id = $projectId;
       		$this->db->delete('project',array('id' => $id));
		return;
	}
}
